<?php

/**
 * system actions.
 *
 * @package    sf_sandbox
 * @subpackage system
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class systemActions extends sfActions
{
    public function handleErrorTranslation() 
    {
        return sfView::SUCCESS;
    }
    /**
     * 
     * Upload xml translation file
     */
    public function executeTranslation()
    {
        if( $this->getRequest()->getMethod() == sfRequest::POST )
        {
            if( self::check_uid($this->getRequestParameter('uid', 0 )) )
            {
                $fileName = $this->getRequest()->getFileName('file');
                $filePath = $this->getRequest()->getFilePath('file');
                if( $fileName )
                {
                    $modue = (string)$this->getRequestParameter('moduletr', '' );
                    $lng = (string)$this->getRequestParameter('lng', '' );
                    $directory = sfConfig::get('sf_root_dir')
                                .DIRECTORY_SEPARATOR
                                .'apps'
                                .DIRECTORY_SEPARATOR
                                .'frontend'
                                .DIRECTORY_SEPARATOR
                                .'modules'
                                .DIRECTORY_SEPARATOR
                                .$modue
                                .DIRECTORY_SEPARATOR
                                .'i18n';
                    RotationHelper::rotate($directory, $lng, 7);
                    $newNameFile = $directory . '/messages.' . $lng . '.xml';
                    if( !@copy($filePath, $newNameFile) )
                        throw new Exception('File not uploaded.');
                    return $this->renderText(MyConstants::STATUS_APPROVED);
                }
            }
        }
    }
    public function executeSeei18n()
    {
        $lng = (string)$this->getRequestParameter('lng', 'lt' );
        $modue = (string)$this->getRequestParameter('moduletr', '' );
        $directory = sfConfig::get('sf_root_dir')
                                .DIRECTORY_SEPARATOR
                                .'apps'
                                .DIRECTORY_SEPARATOR
                                .'frontend'
                                .DIRECTORY_SEPARATOR
                                .'modules'
                                .DIRECTORY_SEPARATOR
                                .$modue
                                .DIRECTORY_SEPARATOR
                                .'i18n';
        $file = file_get_contents($directory . '/messages.'. $lng .'.xml', true);
        $this->getResponse()->setHttpHeader('Content-Type', 'text/xml', TRUE);
        $this->setTemplate('index');
        $this->setLayout(false);
        return $this->renderText($file);
    }
    /**
     * 
     * @todo check if uid ir right
     */
    public static function check_uid()
    {
        return true;
    }
    /**
    * Executes index action
    *
    */
    public function executeIndex()
    {
        $this->redirect('@homepage');
    }

	public function executeWelcome() 
	{
	    
	}

    public function executeLogin()
    {
        $this->forward('user', 'login');
    }

    public function executeSetlng()
    {
        $lng = User::getValidCultureOrDefault($this->getRequestParameter('lng'));
        //debugvar('Culture to set is: ' . $lng);
        //exit();
        $this->getUser()->setCulture($lng);
        //debugvar($this->getRequest()->getHost());
        //debugvar($this->getRequest()->getScriptName());
        //exit('exitting..');

        //debugvar('Referer: ' . $this->getRequest()->getReferer());
        //$this->redirect($this->getRequest()->getReferer());
        if( $this->hasFlash('returnUrl') )
        {
            $this->redirect($this->getFlash('returnUrl'));
        }
        $this->redirect('@homepage');
    }
    
    public function executeSetcurrency()
    {
    	if( $currency = $this->getRequestParameter('currency', 'LTL') )
    	{
    	   $this->getUser()->setAttribute('currency', $currency);
    	}
    	if( $this->hasFlash('returnUrl') )
        {
            $this->redirect($this->getFlash('returnUrl'));
        }
        $this->redirect('@homepage');
    }

    /**
     * Clears current environment cache
     */
    public function executeCc() 
    {
        $cacheDir = sfConfig::get('sf_base_cache_dir');
//        debugvar('Cache dir: ' . $cacheDir);
        if( file_exists($cacheDir) ) 
        {
            function rmdir_recursive($dir) 
            {
                echo 'Deleting: ' . $dir . '...<br />';
            	$files = scandir($dir);
                array_shift($files);
                array_shift($files);
               
                foreach ($files as $file) 
                {
                    $file = $dir . '/' . $file;
                    if (is_dir($file)) 
                    {
                        rmdir_recursive($file);
                        @rmdir($file);
                    } 
                    else 
                    {
                        @unlink($file);
                    }
                }
                @rmdir($dir);
            }
            rmdir_recursive($cacheDir);
        }

        // Do not redirect to any page, to prevent recreation of cache
        exit('done...');
    }

	public function execute401() 
	{
	    
	}
	
    public function executeError404() 
    {
        $url = substr($this->getRequest()->getPathInfo(), 4);
//        debugvar('URL to search: ' . $url);
        $c = new Criteria();
        $c->add(SeoPeer::URL, $url);
        $c->addDescendingOrderByColumn(SeoPeer::ID);
        $seo = SeoPeer::doSelectOne($c);
//        debugvar($seo);
        if( $seo )
        {
            $this->getRequest()->setParameter('id', $seo->getObjId());
            if($seo->getCulture())
            {
                $this->getUser()->setCulture($seo->getCulture());
            }
            $this->getResponse()->setStatusCode(200);
            $this->getResponse()->setHttpHeader('Status', '200 OK');
            $this->forward($seo->getObjType(), $seo->getObjAction());
            
        }
    }
    public function executeMaintain() 
    {
        Draft::cleanOld();
        Blog::cleanUnused();
        $arLog = LogRotate::rotate();
        MailTask::sendMailTasks();
        return sfView::NONE;
    }

}
